package com.weipeep.sys.controller;


import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weipeep.sys.domain.ResetPasswordDO;
import com.weipeep.sys.service.ResetPasswordService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 重置密码表
 * </pre>
 * <small> 2018-07-14 14:10:43 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/resetPassword")
public class ResetPasswordController {
	@Autowired
	private ResetPasswordService resetPasswordService;
	
	@GetMapping()
	@RequiresPermissions("sys:resetPassword:resetPassword")
	String ResetPassword(){
	    return "sys/resetPassword/resetPassword";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:resetPassword:resetPassword")
	public Result<Page<ResetPasswordDO>> list(Integer pageNumber, Integer pageSize, ResetPasswordDO resetPasswordDTO){
		// 查询列表数据
        Page<ResetPasswordDO> page = new Page<>(pageNumber, pageSize);
        
        Wrapper<ResetPasswordDO> wrapper = new EntityWrapper<ResetPasswordDO>(resetPasswordDTO);
        page = resetPasswordService.selectPage(page, wrapper);
        int total = resetPasswordService.selectCount(wrapper);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("sys:resetPassword:add")
	String add(){
	    return "sys/resetPassword/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:resetPassword:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ResetPasswordDO resetPassword = resetPasswordService.selectById(id);
		model.addAttribute("resetPassword", resetPassword);
	    return "sys/resetPassword/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:resetPassword:add")
	public Result<String> save( ResetPasswordDO resetPassword){
		if(resetPasswordService.insert(resetPassword)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:resetPassword:edit")
	public Result<String>  update( ResetPasswordDO resetPassword){
		resetPasswordService.updateById(resetPassword);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:resetPassword:remove")
	public Result<String>  remove( Long id){
		if(resetPasswordService.deleteById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:resetPassword:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		resetPasswordService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
